package com.sunshine.student_register.ui.fragment.register;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.sunshine.student_register.R;
import com.sunshine.student_register.database.register.RegisterEntity;
import com.sunshine.student_register.database.student.StudentEntity;
import com.sunshine.student_register.ui.activity.login.LoginActivity;
import com.sunshine.student_register.ui.activity.main.MainActivity;

import java.util.Calendar;
import java.util.Date;

/**
 * @author sunshine
 * @date 2020/5/30
 * 签到界面
 */
public class RegisterFragment extends Fragment {

    private RegisterViewModel viewModel;
    private MainActivity activity;

    /**
     * 绑定界面
     * @param inflater Inflater类绑定界面
     * @param container 父视图
     * @param savedInstanceState 储存数据
     * @return 界面
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_register, container, false);
    }

    /**
     * 当活动创建后创建，一般操作从这里开始
     * @param savedInstanceState 储存数据
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        viewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        activity = (MainActivity)getActivity();

        if (viewModel.hasLogin()){  //如果已经登录，则加载界面，否则跳转到注册登录界面
            initView();
        }else {
            startActivity(new Intent(activity, LoginActivity.class));
            activity.finish();
        }
    }

    private void initView(){
        Button buttonRegister = getView().findViewById(R.id.button_register);
        final TextView textViewInfo = getView().findViewById(R.id.textView_info);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final StudentEntity studentEntity = viewModel.getStudentEntity();
                final Calendar calendar = Calendar.getInstance();
                MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
                builder.setTitle("提示");
                builder.setMessage(
                        "你将要提交的数据如下：" +
                        "\n姓名：" + studentEntity.getName() +
                        "\n院系：" + studentEntity.getDepartment() +
                        "\n打卡时间：" + calendar.getTime() +
                        "\n确认提交吗？");
                builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        RegisterEntity registerEntity = new RegisterEntity(studentEntity.getId(), calendar.getTimeInMillis());
                        viewModel.toRegister(registerEntity);
                        textViewInfo.setText("签到成功！信息如下：" +
                                        "\n姓名：" + studentEntity.getName() +
                                        "\n院系：" + studentEntity.getDepartment() +
                                        "\n打卡时间：" + calendar.getTime());
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
            }
        });
    }
}
